package org.jeecg.modules.wlhy.hyconfig.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyconfig.service.IHyConfigService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.InitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

 /**
 * @Description: hy_config
 * @Author: jeecg-boot
 * @Date:   2021-08-20
 * @Version: V1.0
 */
@Api(tags="hy_config")
@RestController
@RequestMapping("/hyconfig/hyConfig")
@Slf4j
public class HyConfigController extends JeecgController<HyConfig, IHyConfigService> {
	@Autowired 	private IHyConfigService hyConfigService;
	 @Autowired private IHySettingService settingService;
	 @Autowired private InitService initService;


//	 @Autowired private AppUtils appUtils;
//	@ApiOperation(value="hy_config-分页列表查询", notes="hy_config-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<?> queryPageList(HyConfig hyConfig,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<HyConfig> queryWrapper = QueryGenerator.initQueryWrapper(hyConfig, req.getParameterMap());
//		Page<HyConfig> page = new Page<HyConfig>(pageNo, pageSize);
//		IPage<HyConfig> pageList = hyConfigService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}

	@GetMapping(value = "/cfg")
	public Result<?> getCfg() {
		HyConfig config = HyConfigUtils.getConfig();
		JSONObject cfg = new JSONObject();
		cfg.put("nhST",config.getNhbankIzUse()? "1" : "0");
		cfg.put("pfST",config.getPfIzUse()? "1" : "0");
		cfg.put("zxST",config.getZxIzUse()? "1" : "0");
		cfg.put("tlST" , HyCfgUtil.isTongLianUsed() ? "1" : "0");
		cfg.put("pa" , config.getPingAnIsUse() ? "1" : "0");
		cfg.put("zhengzhou" , config.getZhengzhouIsUse() ? "1" : "0");
		cfg.put("payPlan" , HyCfgUtil.isUsed2("pay_plan") ? "1" : "0");//支付计划
		cfg.put("showSinglePaiDanBtn" , HyCfgUtil.isUsed2("showSinglePaiDanBtn") ? "1" : "0");//单个派单按钮显示控制
		cfg.put("isAllowDriverMultipleOrders" , HyCfgUtil.isUsed2("driver_multiple_orders") ? "1" : "0");//单个派单按钮显示控制
		return Result.OK(cfg);
	}


	@AutoLog(value = "hy_config-编辑")
	@ApiOperation(value="hy_config-编辑", notes="hy_config-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("config:params")
	public Result<?> edit(@RequestBody HyConfig hyConfig) throws Exception {

		if (hyConfig.getZjxlIzUse() && hyConfig.getXzApiIzUse()) {
			return Result.error("请勿同时开启中交兴路及中交慧联服务");
		}

		if (hyConfig.getAliyunSmsIsUse()!=null){
			if (hyConfig.getSmsIzUse()&&hyConfig.getAliyunSmsIsUse()){
				return Result.error("请勿同时开启华为云短信及阿里云短信服务");
			}
		}

		String pwd = hyConfig.getPwd();
		if (StringUtils.isEmpty(pwd)) {
			return Result.error("密码不能为空");
		}

		String syspwd = settingService.getSettingValueByTag2("syspwd");
		if (StringUtils.isEmpty(syspwd)) {
			return Result.error("密码错误！");
		}
		String encryptPwd = PasswordUtil.encrypt("pd", pwd, PasswordUtil.BSalt);
		if (!StringUtils.equals(encryptPwd, syspwd)) {
			return Result.error("密码错误！");
		}
		hyConfigService.updateById(hyConfig);
		HyConfigUtils.setConfig(hyConfig);
		initService.setPayCenterConfig(hyConfig);
		//initService.Initbpt(hyConfig);
		return Result.OK("编辑成功!");
	}

	@ApiOperation(value="hy_config-通过id查询", notes="hy_config-通过id查询")
	@GetMapping(value = "/queryById")
	@RequiresPermissions("config:params")
	public Result<?> queryById(@RequestParam(name="pwd",defaultValue = "") String pwd) {
		pwd = pwd.trim();
		if(StringUtils.isEmpty(pwd)){
			return Result.error("密码不能为空");
		}

		String syspwd = settingService.getSettingValueByTag2("syspwd");
		if(StringUtils.isEmpty(syspwd)){
			return Result.error("密码错误！");
		}
		String encryptPwd = PasswordUtil.encrypt("pd", pwd, PasswordUtil.BSalt);
		if (!StringUtils.equals(encryptPwd, syspwd)) {
			return Result.error("密码错误！");
		}

		HyConfig hyConfig = hyConfigService.getById("1");
		if(hyConfig==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyConfig);
	}

}
